﻿using System;
using System.Globalization;
using System.Threading;
using System.Web.Mvc;
using System.Web.Routing;
using SusuCMS.Globalization;
using System.Web;
using System.Linq;
using System.Web.Caching;
using System.Configuration;

namespace SusuCMS.Web
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Page_Default",
                "{*url}",
                new { controller = "Home", action = "Index" },
                new[] { "SusuCMS.Web.Controllers" }
            );

            ModelMetadataProviders.Current = new LocalizeDataAnnotationsModelMetadataProvider();

            ModelValidatorProviders.Providers.RemoveAt(0);
            ModelValidatorProviders.Providers.Insert(0, new LocalizeDataAnnotationsModelValidatorProvider());
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Application_BeginRequest(object o, EventArgs e)
        {
            //if (Request.Cookies["CultureName"] != null)
            //{
            //    var cultureName = Request.Cookies["CultureName"].Value;

            //    Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
            //    Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
            //}
        }
    }
}